package com.haidong.controller;

import com.haidong.domain.Book;
import com.haidong.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping
    public Result save(@RequestBody Book book){
        boolean flag = bookService.save(book);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @PutMapping
    public Result update(@RequestBody Book book){
        boolean flag = bookService.update(book);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean flag = bookService.delete(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
//        // 模拟一个业务异常,包装成自定义异常
//        if(id==1){
//            throw new BusinessException(Code.BUSINESS_ERR,"不要乱抛异常");
//        }
//        // 模拟一个系统异常
//        try {
//            int i = 1/0;
//        } catch (Exception e) {
//            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"系统出错",e);
//        }
        Book book = bookService.getById(id);
        Integer code = book != null ? Code.GET_OK:Code.GET_ERR;
        String msg = book != null ? "":"数据查询失败,请重试";
        return new Result(code,book,msg);
    }

    @GetMapping
    public Result getAll(){
        List<Book> books = bookService.getAll();
        Integer code = books.size()!=0 ? Code.GET_OK:Code.GET_ERR;
        String msg = books.size()!=0 ? "":"数据查询失败";
        return new Result(code,books,msg);
    }
}
